/*
	Copyright (C) 2009 jungil Han <jungil.Han@gmail.com>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mkAboutDialog.h"

#include <QUrl>
#include <QDate>
#include <QTime>
#include <QLabel>
#include <QDesktopServices>

mkAboutDialog::mkAboutDialog( QWidget *a_pParent, Qt::WindowFlags a_eFlags ) : mkDialog( a_pParent, a_eFlags )
{
	m_pCloseButton = NULL;
	m_pProjectHomeLinkButton = NULL;
	m_pBlogLinkButton = NULL;

	m_pImageLabel = NULL;
	m_pHeadTextLabel = NULL;
	m_pBodyTextLabel = NULL;
}

mkAboutDialog::~mkAboutDialog()
{
	MK_DELETE( m_pImageLabel );
	MK_DELETE( m_pHeadTextLabel );
	MK_DELETE( m_pBodyTextLabel );

	MK_DELETE( m_pProjectHomeLinkButton );
	MK_DELETE( m_pBlogLinkButton );
	MK_DELETE( m_pCloseButton );
}

void mkAboutDialog::OnInitDialog()
{
	/* Set the dialog's title */
	setWindowTitle( QString( "About Monkey Hotkey" ));

	/* Set the dialog's size */
	resize( 460, 140 );
	setMaximumSize( 460, 140 );
	setMinimumSize( 460, 140 );
	
	static QString strDate = GetCurrentDate();

	if ( !m_pImageLabel ) {
		m_pImageLabel = new QLabel( this );
		m_pImageLabel->setPixmap( QPixmap( QString( ":/About/jihan.png" )));
		m_pImageLabel->setGeometry( 10, 10, 120, 118 );
	}

	QPalette textLabelPalette;
	textLabelPalette.setBrush( QPalette::WindowText, QBrush ( QColor(255, 255, 255 )));
	
	if ( !m_pHeadTextLabel ) {
		m_pHeadTextLabel = new QLabel( this );
		m_pHeadTextLabel->setPalette( textLabelPalette );
		m_pHeadTextLabel->setGeometry( 140, 10, 300, 20 );
		m_pHeadTextLabel->setAlignment( Qt::AlignLeft );
		m_pHeadTextLabel->setFont( QFont( MONKEY_FONT, 10 ));
		m_pHeadTextLabel->setText( QString("Monkey Hotkey") + " " + QString( MONKEY_VERSION ));
	}

	if ( !m_pBodyTextLabel ) {
		m_pBodyTextLabel = new QLabel( this );
		m_pBodyTextLabel->setPalette( textLabelPalette );
		m_pBodyTextLabel->setGeometry( 140, 40, 300, 100 );
		m_pBodyTextLabel->setAlignment( Qt::AlignLeft );
		m_pBodyTextLabel->setTextInteractionFlags( Qt::TextSelectableByMouse );
		m_pBodyTextLabel->setFont( QFont( MONKEY_FONT, 8 ));
		m_pBodyTextLabel->setText( QString( "Based on Qt 4.5.2 (LGPL, 32 bit)\n") +
								QString( "Built on " ) + MONKEY_RELEASE_DATE + QString("\n") +
								QString( "[Project Home] code.google.com/p/monkeyhotkey\n") +
								QString( "[Blog] linuxspot.tistory.com\n\n") +
								QString( "Maintainer jungil.Han@gmail.com\n"));
	}
	
	if ( !m_pProjectHomeLinkButton ) {
		m_pProjectHomeLinkButton = new mkButton( this );
		m_pProjectHomeLinkButton->SetBGImage( QString( ":/About/about_link_btn_bg_default.png" ), QString( ":/About/about_link_btn_bg_over.png" ), QString( ":/About/about_link_btn_bg_click.png" ));
		m_pProjectHomeLinkButton->setGeometry( 435, 69, 16, 16 );
		connect( m_pProjectHomeLinkButton, SIGNAL( clicked() ), this, SLOT( SlotProjectHomeLinkClicked() ));
	}
	
	if ( !m_pBlogLinkButton ) {
		m_pBlogLinkButton = new mkButton( this );
		m_pBlogLinkButton->SetBGImage( QString( ":/About/about_link_btn_bg_default.png" ), QString( ":/About/about_link_btn_bg_over.png" ), QString( ":/About/about_link_btn_bg_click.png" ));
		m_pBlogLinkButton->setGeometry( 304, 85, 16, 16 );
		connect( m_pBlogLinkButton, SIGNAL( clicked() ), this, SLOT( SlotBlogLinkClicked() ));
	}

	if ( !m_pCloseButton ) {
		m_pCloseButton = new mkButton( this );
		m_pCloseButton->SetBGImage( QString( ":/About/about_close_btn_bg_default.png" ), QString( ":/About/about_close_btn_bg_over.png" ), QString( ":/About/about_close_btn_bg_click.png" ));
		m_pCloseButton->setGeometry( 390, 105, 60, 25 );
		QRect fontRect( 0, 0, 60, 25 );
		m_pCloseButton->SetText( QString( "Close" ), &fontRect, 8, Qt::AlignCenter );
		connect( m_pCloseButton, SIGNAL( clicked() ), this, SLOT( accept() ));

		m_pCloseButton->setFocus();
	}
}

void mkAboutDialog::SlotProjectHomeLinkClicked()
{
	QDesktopServices::openUrl( QUrl( "http://code.google.com/p/monkeyhotkey" ));
}

void mkAboutDialog::SlotBlogLinkClicked()
{
	QDesktopServices::openUrl( QUrl( "http://linuxspot.tistory.com/109" ));
}

/* Not be used */
QString mkAboutDialog::GetCurrentDate()
{
	QDate date = QDate::currentDate();
	QString strDate = date.toString( "MMMM dd yyyy" );

	QTime time = QTime::currentTime();
    QString strTime = time.toString("HH:mm:ss");

	strDate += QString( " at " ) + strTime;
	return strDate;
}
